package com.ehome.emergency.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;
import java.util.Properties;

/**
 * BasicFramework
 *
 * @author: haoxiaolei
 * @date: 2016-05-10 19:33
 * @desc: ApplicationProperties工具类
 */
public class ApplicationPropertiesUtils {
    private static final Logger LOG = LoggerFactory.getLogger(ApplicationPropertiesUtils.class);

    private static String path;
    private static Properties props;

    //静态块 初始化
    static {
        props = new Properties();
        path = ApplicationPropertiesUtils.class.getClassLoader().getResource("application.properties") + "";
    }

    /**
     * 获取属性文件的值
     *
     * @param key
     */
    public static String getValue(String key) {
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(new File(new URI(path))));
            props.load(in);
            return props.getProperty(key);
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error("没有此属性值，或读取文件错误！");
            return "";
        }
    }
}
